/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.Log4jLogMapper;
import com.ybkj.daijia.server.model.Log4JLog;
import com.ybkj.daijia.server.service.Log4JLogService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhyu
 * @date 2019-06-27 09:13:23
 */
@Service
public class DefaultLog4JLogService implements Log4JLogService {

    @Autowired
    private Log4jLogMapper mapper;


    /* (non-Javadoc)
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    public Page<Log4JLog> selectByMap(Map<String, Object> param) {

        String account = "shine";

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

//        Map<String, Object> params = new HashMap<String, Object>();
//
//        params.put("offset", Pager.getOffset());
//        params.put("size", Pager.getLimit());
/***
 * size = 0
 * 0 : 0-9
 * 1: 10-19
 * 2: 20-29
 */
        Integer page = Integer.valueOf(param.get("page").toString());
        Integer size = Integer.valueOf(param.get("size").toString());
        Integer offset = page * size;
        Pageable pageable = new PageRequest(offset, size);
        param.put("offset", offset);
        List<Log4JLog> content = mapper.selectByMap(param);
        Long total = mapper.countByMap(param);
        return new PageImpl<Log4JLog>(content, pageable, total);

    }

//    public static void main(String[] args) {
//        Map<String, Object> map = new HashMap<>();
//        Integer xx = 3;
//        map.put("test",xx);
//        Integer ll = Integer.valueOf( map.get("test").toString());
//        System.out.println(ll);
//    }


    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Log4JLog> export(HttpSession session, Map<String, Object> param, Long timestamp) {
        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Map<String, Object> params = new HashMap<String, Object>();

        Long total = mapper.countByMap(params);
        List<Log4JLog> totalContent = new ArrayList<Log4JLog>();

        Long forTimes = total / 1000;
        if (total % 1000 != 0) {
            ++forTimes;
        }

        for (int i = 0; i < forTimes; i++) {

            params.put("offset", i * 1000);
            params.put("size", 1000);
            List<Log4JLog> content = mapper.selectByMap(params);
            totalContent.addAll(content);

            // 进度
            session.setAttribute("exportLog4JLogProcess_" + timestamp,
                Double.valueOf(i + 1) / Double.valueOf(forTimes));
            // 进度

        }

        // 进度
        session.removeAttribute("exportLog4JLogProcess_" + timestamp);
        // 进度
        return totalContent;
    }

    @Override
    public int autoDeleteLogs() {
        return 0;
    }

}